//
//  Loadingview.m
//  DDKelai
//
//  Created by 宋华 on 16/4/14.
//  Copyright © 2016年 Admin. All rights reserved.
//

#import "Loadingview.h"

#define degreesToRadinas(x) (M_PI * (x)/180.0)

@interface Loadingview() {
    BOOL animate;

}

@property (weak, nonatomic) IBOutlet UIImageView *refreshImageView;


//- (IBAction)onTryAgainBtnTouchUpInside:(id)sender;

@end

@implementation Loadingview

- (void)startAnimating {
    animate = YES;
    self.refreshImageView.hidden = NO;
    self.tryAgainBtn.hidden = YES;
    [self loading];
    if (!self.superview && self.parentView) {
        [self.parentView addSubview:self];
    }
}

- (void)stopAnimating {
    animate = NO;
    [self.refreshImageView.layer removeAllAnimations];
    self.refreshImageView.hidden = YES;
    self.tryAgainBtn.hidden = NO;
}


- (void)loading {
    [UIView animateWithDuration:0.015f
                          delay:0.0f
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         self.refreshImageView.transform = CGAffineTransformRotate(self.refreshImageView.transform, degreesToRadinas(4));
                     }
                     completion:^(BOOL finished) {
                         if (animate && finished)
                         {
                             [self loading];
                         }
                     }];
    
}

- (IBAction)onTryAgainBtnTouchUpInside:(id)sender {
    if (self.tryAgainBlock) {
        self.tryAgainBlock();
    }
}

@end
